mui.plusReady(function() {
	var user = app.getState();
	var uid = user.user_id;
	var myWallet = ethscan.getWallet('eth');
	var self = plus.webview.currentWebview();
	var type = self.type;
	//console.log(type);
	mui.init({
		pullRefresh: {
			container: '#pullrefresh',
			up: {
				contentrefresh: '',
				callback: pullupRefresh
			}
		}
	});

	var count = 0;
	/**
	 * 上拉加载具体业务实现
	 */
	var page = 0;
	var p = 0;

	function pullupRefresh() {
		//setTimeout(function() {
		mui('#pullrefresh').pullRefresh().endPullupToRefresh((count++ > 10));
		var table = document.body.querySelector('.mui-table-view');
		var cells = document.body.querySelectorAll('.mui-table-view-cell');
		count = cells.length;
		var size = 20;
		if(count == 0 || count < size) {
			page = 0;
		} else {
			page = Math.ceil(count / size);
		}
		//console.log(page);
		if(page + 1 == p) {
			return;
		}
		p = page + 1;
		//console.log(p);
		if(type == 'buy') {
			var url = apiurl + 'order/tradeWaitCheck?page=' + p + '&size=' + size;
		} else {
			var url = apiurl + 'order/tradeWaitConfirm?page=' + p + '&size=' + size;
		}
		//console.log(url);
		mui.getJSON(url, function(res) {
			console.log(JSON.stringify(res));
			if(res.data && res.data.length > 0) {
				document.getElementById('area').innerHTML = document.getElementById('area').innerHTML + template("tpl", {
					data: res.data
				});
			} else {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(true); //参数为true代表没有更多数据了。
			}
		});
		/*
		var str = '';
		for(var i = cells.length, len = i + 20; i < len; i++) {
			str += '<li class="mui-table-view-cell"><a class="mui-navigate-right">Item ' + (i + 1) + '</a></li>';
			table.appendChild(str);
		}*/
		//}, 500);
	}
	mui("#area").on('tap', '.compay', function() {
		//	console.log(this.id);
		var me = this;

		var url = apiurl + 'order/tradeDetail';
		//console.log(uid);
		var postdata = {
			id: this.id,
			uid: uid,
			token: md5(token)
		};
		console.log(JSON.stringify(postdata));
		console.log(url);
		mui.post(url, postdata, function(res) {
			//console.log(JSON.stringify(res));
			//return;
			if(!res || !res.status || res.status != 1) {
				mui.alert("已审核或数据不存在！");
				return;
			} else {
				var data = res.data;
				var status = data.status * 1;
				console.log(JSON.stringify(data));
				//return;
				//审核挂卖
				if(status == 4) {
					plus.nativeUI.showWaiting("后台处理中...");
					//console.log(JSON.stringify(postdata));
					var postdata = {
						id: data.id,
						uid: data.user_id,
						token: md5(token)
					};
					mui.post(apiurl + 'order/tradeCheck', postdata, function(rt) {
						//mui.alert(rt.msg);
						console.log(JSON.stringify(rt));
						plus.nativeUI.closeWaiting();
						if(rt.status && rt.status > 0) {
							mui.toast('审核成功！');
							document.getElementById('status' + me.id).innerHTML = "已审核";
							me.parentNode.removeChild(me);
						} else {
							mui.alert(rt.msg);
						}

						/*
						if(!rt || !rt.status || rt.status < 1) {
							return;
						} else {
							document.getElementById('status' + data.id).innerHTML = "已审核";
							me.parentNode.removeChild(me);

						}*/
					});
				}
				if(status == 8) {
					//审核支付
					var coindata = ethscan.getDefCoin();
					//console.log(res.wallet_addr);
					//return;
					var postdata = {
						check_hash: '',
						id: data.id,
						uid: data.user_id
					};
					plus.nativeUI.showWaiting("后台处理中...");
					mui.post(apiurl + 'order/tradeConvert', postdata, function(rt) {
						plus.nativeUI.closeWaiting();
						ethscan.payToAddrEth(data.buy_address, coindata['ctype'], coindata, data.trade_money.toString(), function(obj, addr) {
							console.log(JSON.stringify(obj));
							if(!obj || !obj.hash) {
								mui.alert('付款失败！');
							} else {
								document.getElementById('status' + data.id).innerHTML = "已支付";
								me.parentNode.removeChild(me);
								//data.hash = obj.hash;
								var postdata = {
									check_hash: obj.hash,
									id: data.id,
									uid: data.user_id
								};
								plus.nativeUI.showWaiting("后台完结中...");
								mui.post(apiurl + 'order/tradeConvert', postdata, function(rt) {
									console.log(JSON.stringify(rt))
									plus.nativeUI.closeWaiting();
								});
								mui.toast('付款成功！');
							}
						}, function(err) {
							console.log(JSON.stringify(err));
							mui.alert('付款失败！');
						});
					});

				}

			}
		});
	});
	if(mui.os.plus) {
		setTimeout(function() {
			document.getElementById('pullrefresh').style.paddingTop = '50px';
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 1000);
	} else {
		mui('#pullrefresh').pullRefresh().pullupLoading();
	}
});